﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class CourseFaculty_AllCourseFacultyInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        MaintainScrollPositionOnPostBack = true;
        if (!IsPostBack)
        {
            FillCourseFacultyInGrid();
        }

    }
    #region CourseRegistrationDataBase Calls
    private DataTable FetchAllCourseFacultyInfo()
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlDataAdapter dAd = new SqlDataAdapter();
        SqlCommand dCmd = new SqlCommand();
        DataTable dTable = new DataTable();
        try
        {
            dCmd.CommandText = "SelectAllCourseFaculties";
            dCmd.CommandType = CommandType.StoredProcedure;
            dCmd.Connection = conn;
            dAd.SelectCommand = dCmd;
            // now open the connection
            conn.Open();
            dAd.Fill(dTable);
            conn.Close(); // close the connection

            return dTable;
        }
        catch (Exception e)
        {
            throw e;
        }

        finally
        {
            dCmd.Clone();
            dAd.Dispose();
            conn.Dispose();
        }
    }
    private DataTable SearchCourseFacultyFacultyName(string name)
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlDataAdapter dAd = new SqlDataAdapter();
        SqlCommand dCmd = new SqlCommand();
        DataTable dTable = new DataTable();
        try
        {
            dCmd.CommandText = "CourseFacultyFacultySearch";
            dCmd.CommandType = CommandType.StoredProcedure;
            dCmd.Connection = conn;
            SqlParameter[] prms = new SqlParameter[1];
            prms[0] = new SqlParameter("@FacultyName", SqlDbType.VarChar, 40);
            prms[0].Value = name;
            dCmd.Parameters.AddRange(prms);
            dAd.SelectCommand = dCmd;
            // now open the connection
            conn.Open();
            dAd.Fill(dTable);
            conn.Close(); // close the connection

            return dTable;
        }
        catch (Exception e)
        {
            throw e;
        }

        finally
        {
            dCmd.Clone();
            dAd.Dispose();
            conn.Dispose();
        }
    }
    private DataTable SearchCourseFacultyCourseId(string courseId)
    {
        string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
        SqlConnection conn = new SqlConnection(connStr);
        SqlDataAdapter dAd = new SqlDataAdapter();
        SqlCommand dCmd = new SqlCommand();
        DataTable dTable = new DataTable();
        try
        {
            dCmd.CommandText = "CourseFacultyCourseSearch";
            dCmd.CommandType = CommandType.StoredProcedure;
            dCmd.Connection = conn;
            SqlParameter[] prms = new SqlParameter[1];
            prms[0] = new SqlParameter("@CourseId", SqlDbType.VarChar, 40);
            prms[0].Value = courseId;
            dCmd.Parameters.AddRange(prms);
            dAd.SelectCommand = dCmd;
            // now open the connection
            conn.Open();
            dAd.Fill(dTable);
            conn.Close(); // close the connection

            return dTable;
        }
        catch (Exception e)
        {
            throw e;
        }

        finally
        {
            dCmd.Clone();
            dAd.Dispose();
            conn.Dispose();
        }
    }
    #endregion
    private void FillCourseFacultyInGrid()
    {
        try
        {
            DataTable dTable = FetchAllCourseFacultyInfo(); ;
            if (dTable.Rows.Count > 0)
            {
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
            }
            else
            {
                dTable.Rows.Add(dTable.NewRow());
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
                int TotalColumns = CourseFacultyInfoGridView.Rows[0].Cells.Count;
                CourseFacultyInfoGridView.Rows[0].Cells.Clear();
                CourseFacultyInfoGridView.Rows[0].Cells.Add(new TableCell());
                CourseFacultyInfoGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                CourseFacultyInfoGridView.Rows[0].Cells[0].Text = "No Record Found";
                Master.MasterMessageBoxText = Master.MasterMessageBoxText + "Operation Completed Sucessfully";
            }
        }
        catch (Exception e)
        {
            Master.MasterMessageBoxText = Master.MasterMessageBoxText + "--ERROR: " + e.Message;
        }
    }
    protected void CourseFacNameSearchButton_Click(object sender, EventArgs e)
    {
        string name = CourseFacultyNameTxtBx.Text;

        try
        {
            DataTable dTable = SearchCourseFacultyFacultyName(name);
            if (dTable.Rows.Count > 0)
            {
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
            }
            else
            {
                dTable.Rows.Add(dTable.NewRow());
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
                int TotalColumns = CourseFacultyInfoGridView.Rows[0].Cells.Count;
                CourseFacultyInfoGridView.Rows[0].Cells.Clear();
                CourseFacultyInfoGridView.Rows[0].Cells.Add(new TableCell());
                CourseFacultyInfoGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                CourseFacultyInfoGridView.Rows[0].Cells[0].Text = "No Record Found";
                Master.MasterMessageBoxText = Master.MasterMessageBoxText + "Operation Completed Sucessfully";
            }
        }
        catch (Exception ex)
        {
            Master.MasterMessageBoxText = Master.MasterMessageBoxText + "--ERROR: " + ex.Message;
        }
    }
    protected void CourseFacuCourseIdButton_Click(object sender, EventArgs e)
    {
        string courseId = CourseFacCourseIdTextBx.Text;

        try
        {
            DataTable dTable = SearchCourseFacultyCourseId(courseId);
            if (dTable.Rows.Count > 0)
            {
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
            }
            else
            {
                dTable.Rows.Add(dTable.NewRow());
                CourseFacultyInfoGridView.DataSource = dTable;
                CourseFacultyInfoGridView.DataBind();
                int TotalColumns = CourseFacultyInfoGridView.Rows[0].Cells.Count;
                CourseFacultyInfoGridView.Rows[0].Cells.Clear();
                CourseFacultyInfoGridView.Rows[0].Cells.Add(new TableCell());
                CourseFacultyInfoGridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                CourseFacultyInfoGridView.Rows[0].Cells[0].Text = "No Record Found";
                Master.MasterMessageBoxText = Master.MasterMessageBoxText + "Operation Completed Sucessfully";
            }
        }
        catch (Exception ex)
        {
            Master.MasterMessageBoxText = Master.MasterMessageBoxText + "--ERROR: " + ex.Message;
        }
    }
}